package com.example.shop.controller;

import com.example.shop.domain.Agreement;
import com.example.shop.domain.Goods;
import com.example.shop.domain.Result;
import com.example.shop.service.AgreementService;
import com.example.shop.service.GoodsService;
import com.example.shop.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @projectName: travel
 * @package: com.example.travel.controller
 * @className: OutlayController
 * @author: Liqian
 * @description: TODO
 * @date: 2024/4/12 15:26
 * @version: 1.0
 */
@RestController
@RequestMapping("/market")
@Validated
@Slf4j
@Tag(name = "市场", description = "这是描述")

public class AgreementController {


    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    @Operation(summary = "查询列表")
    public Result list(@RequestParam(value = "name", required = false) String name) {
        // 如果 name 参数为空，则将其设为 null 或空字符串，以便在服务层进行处理
        String queryName = (name != null && !name.isBlank()) ? name : null;
        return Result.success(goodsService.selectByName(queryName));
    }

    @Autowired
    private AgreementService agreementService;

    @PostMapping("/add")
    @Operation(summary = "想要")
    public Result add(@RequestBody Goods goods) {
        // 如果 name 参数为空，则将其设为 null 或空字符串，以便在服务层进行处理
        List<Goods> list = goodsService.selectOne(goods);
        System.out.println(list.get(0));
        goods = list.get(0);
        if (goods.getStatus() == 0) {
            int agreementId = agreementService.borrow(goods);
            goods.setAgreementId(agreementId);
            goods.setStatus(1);
            goodsService.update(goods);
            return Result.success("操作成功");
        }

        return Result.error("该商品已出租");
    }

    @PostMapping("/delete")
    @Operation(summary = "不想要")
    public Result delete(@RequestBody Goods goods) {
        // 如果 name 参数为空，则将其设为 null 或空字符串，以便在服务层进行处理
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Goods> list = goodsService.selectOne(goods);
        goods = list.get(0);
        Agreement agreement = new Agreement();
        agreement.setId(goods.getAgreementId());
        Agreement agreement1 = agreementService.selectById(agreement);
        if (agreement1.getUserIn() == userId) {
            agreementService.delete(agreement1);
            goods.setAgreementId(0);
            goods.setStatus(0);
            goodsService.update(goods);
            return Result.success();
        }
        return Result.error("不存在该订单");
    }


    @PutMapping("/takeBack")
    @Operation(summary = "收回")
    public Result takeBack(@RequestBody Goods goods) {
        // 如果 name 参数为空，则将其设为 null 或空字符串，以便在服务层进行处理
        agreementService.takeBack(goods);

        goods.setStatus(0);
        goods.setAgreementId(null);
        goodsService.update(goods);

        return Result.success(agreementService.getInInf(goods));
    }

    @PutMapping("/out")
    @Operation(summary = "租出")
    public Result out(@RequestBody Goods goods) {
        // 如果 name 参数为空，则将其设为 null 或空字符串，以便在服务层进行处理

        agreementService.lend(goods);
        goods.setStatus(2);
        goodsService.update(goods);
        return Result.success(agreementService.getInInf(goods));
    }

    @PutMapping("/returnGoods")
    @Operation(summary = "归还")
    public Result returnGoods(@RequestParam(value = "agreementId", required = true) String agreementId) {
        // 如果 name 参数为空，则将其设为 null 或空字符串，以便在服务层进行处理
        agreementService.returnGoods(Integer.parseInt(agreementId));
        return Result.success();
    }

    @GetMapping("/getInInf")
    @Operation(summary = "获取租客信息")
    public Result getInInf(@RequestParam(value = "id", required = true) String id) {
        // 如果 name 参数为空，则将其设为 null 或空字符串，以便在服务层进行处理
        Goods goods = new Goods();
        goods.setId(Integer.valueOf(id));
        return Result.success(agreementService.getInInf(goods));
    }

    @GetMapping("/getAgreetInf")
    @Operation(summary = "获取订单信息")
    public Result getAgreeInf(@RequestParam(value = "id", required = true) String id) {
        // 如果 name 参数为空，则将其设为 null 或空字符串，以便在服务层进行处理
        Goods goods = new Goods();
        goods.setId(Integer.valueOf(id));
        return Result.success(agreementService.getAgreementInf(goods));
    }


    @GetMapping("/selectById")
    @Operation(summary = "通过id查询订单信息")
    public Result selectById(@RequestParam(value = "agreementId", required = true) String agreementId) {
        Agreement agreement = new Agreement();
        agreement.setId(Integer.valueOf(agreementId));
        return Result.success(agreementService.selectById(agreement));
    }

    @GetMapping("/list_in")
    @Operation(summary = "查询租入列表")
    public Result list_in() {
        return Result.success(agreementService.selectListIn());
    }

}
